﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Emc.Documentum.FS.Services.Core;
using Emc.Documentum.FS.DataModel.Core;
using Emc.Documentum.FS.DataModel.Core.Query;
using Emc.Documentum.FS.DataModel.Core.Properties;
using Emc.Documentum.FS.DataModel.Core.Profiles;
using Emc.Documentum.FS.Runtime;
using Emc.Documentum.FS.Runtime.Context;
using Emc.Documentum.FS.DataModel.Core.Context;
using Emc.Documentum.FS.DataModel.Core.Content;

namespace CW_Apply_Permissions
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {

        private IQueryService queryService;
        private IObjectService objectService;


        public Window1()
        {
            InitializeComponent();
        }

        private void m_ApplyPermissionsButton_Click(object sender, RoutedEventArgs e)
        {
            initializeContext();
            ServiceFactory serviceFactory = ServiceFactory.Instance;
            objectService = serviceFactory.GetRemoteService<IObjectService>(serviceContext);
            

            try
            {
                ObjectIdentitySet objIdSet = new ObjectIdentitySet();
                ObjectIdentity objId = new ObjectIdentity();
                objId.RepositoryName = "DMS";
                objId.ValueType = ObjectIdentityType.OBJECT_ID;
                ObjectId rObjId = new ObjectId("090008ae800b2555");
                objId.Value = rObjId;
                objIdSet.AddIdentity(objId);

                OperationOptions opOpts = new OperationOptions();
                
                List<ObjectContentSet> listOfUrls = objectService.GetObjectContentUrls(objIdSet);

                ObjectContentSet objSet = listOfUrls[0];
                UrlContent urlObj = (UrlContent)objSet.Contents[0];
                String url = urlObj.Url;
                
                


            }
            catch (ServiceException ex)
            {
                //Console.WriteLine(e.Message);
                MessageBox.Show(ex.ToString());
            }


        }

        private void initializeContext()
        {
            ContextFactory contextFactory = ContextFactory.Instance;
            serviceContext = contextFactory.NewContext();

            RepositoryIdentity repoId = new RepositoryIdentity();
            RepositoryIdentity repositoryIdentity =
                new RepositoryIdentity(defaultRepository, userName, password, "");
            serviceContext.AddIdentity(repositoryIdentity);

            ContentTransferProfile contentTransferProfile = new ContentTransferProfile();
            contentTransferProfile.TransferMode = ContentTransferMode.MTOM;
            contentTransferProfile.Geolocation = "Toronto";
            serviceContext.SetProfile(contentTransferProfile);

            if (!String.IsNullOrEmpty(secondaryRepository))
            {
                RepositoryIdentity repoId1 = new RepositoryIdentity();
                repoId1.RepositoryName = secondaryRepository;
                repoId1.UserName = userName;
                repoId1.Password = password;
                serviceContext.AddIdentity(repoId1);
            }
        }

        private string defaultRepository = "DMS";
        private string secondaryRepository = "DMS";
        private string userName = "your username";
        private string password = "your password";
        private IServiceContext serviceContext;

    }
}
